package org.luxor.eshop.account.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.luxor.commons.core.exception.TransactionException;
import org.luxor.eshop.account.entity.AccountEntity;
import org.luxor.eshop.account.entity.dto.PaymentReqDto;
import org.luxor.eshop.account.entity.dto.ReceiptReqDto;
import org.luxor.eshop.account.mapper.AccountMapper;
import org.luxor.eshop.account.service.AccountService;
import org.luxor.eshop.account.service.PaymentTccAction;
import org.luxor.eshop.account.service.ReceiptTccAction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 帐户服务
 *
 * @author Mr.Yan  @date: 2020/11/25
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private PaymentTccAction paymentTccAction;
    @Resource
    private ReceiptTccAction receiptTccAction;

    /**
     * 获取账户信息
     */
    @Override
    public AccountEntity get(String account) {
        AccountEntity info = accountMapper.selectByAccount(account);
        return info;
    }

    /**
     * 付款
     */
    @Override
    @GlobalTransactional(rollbackFor = TransactionException.class)
    public void payment(PaymentReqDto paymentReq) {
        // 调用TCC付款,第一阶段方法
        paymentTccAction.prepare(null, paymentReq.getBillCode()
                , paymentReq.getPaymentAccount()
                , paymentReq.getAmounts());
    }

    /**
     * 收款
     */
    @Override
    @GlobalTransactional(rollbackFor = TransactionException.class)
    public void receipt(ReceiptReqDto receiptReq) {
        // 调用TCC收款,第一阶段方法
        receiptTccAction.prepare(null, receiptReq.getBillCode()
                , receiptReq.getReceiptAccount()
                , receiptReq.getAmounts());
    }
}
